﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Globalization;

namespace HyperPin_Database_Editor
{
    public  class HPSettingsPanel : UserControl
    {
        protected string currentSettingsFileName;
        protected string currentSettingsSection;

        public virtual void doLoadSettings()
        {
            // Implemented by subclass to load settings from INI file
        }

        public virtual void doSaveSettings()
        {
            // Implemented by subclass to save settings to INI file
        }

        public void setSettingsFileName(string settingsFileName)
        {
            currentSettingsFileName = settingsFileName;
        }

        public void setSettingsSection(string section)
        {
            currentSettingsSection = section;
        }

        public void writeSetting(string key, string value)
        {
            if (currentSettingsFileName != null)
            {
                IniFile settingsINI = new IniFile(currentSettingsFileName);
                settingsINI.IniWriteValue(currentSettingsSection, key, " " + value);
            }
        }

        public void writeSettingInt(string key, int value)
        {
            writeSetting(key, Convert.ToString(value));
        }

        public void writeSettingBool(string key, bool value)
        {
            string valueString = (value == true) ? "true" : "false";
            writeSetting(key, valueString);
        }

        public void writeSettingDecimal(string key, decimal value)
        {
            writeSetting(key, Convert.ToString(value));
        }

        public void writeSettingColor(string key, Color color)
        {
            int argb = color.ToArgb();
            string hexValue = Convert.ToString(argb, 16);
            string outputValue = String.Format("0x{0}", hexValue.Substring(2));
            writeSetting(key, outputValue);
        }

        public string[] readSettings()
        {
            if (currentSettingsFileName != null)
            {
                IniFile settingsINI = new IniFile(currentSettingsFileName);
                return settingsINI.IniReadSection(currentSettingsSection);
            }

            return null;
        }

        public string readSetting(string key, string defaultValue)
        {
            string returnValue = defaultValue;
            if (currentSettingsFileName != null)
            {
                IniFile settingsINI = new IniFile(currentSettingsFileName);
                string settingValue = settingsINI.IniReadValue(currentSettingsSection, key);
                if (settingValue != null && settingValue != "")
                {
                    return settingValue;
                }
            }

            return returnValue;
        }

        public bool readSettingBool(string key, bool value)
        {
            string defaultValueString = (value == true) ? "true" : "false";
            string stringValue = readSetting(key, defaultValueString);
            if (stringValue == "true")
                return true;

            return false;
        }

        public int readSettingInt(string key, int defaultValue)
        {
            string defaultString = Convert.ToString(defaultValue);
            string stringValue = readSetting(key, defaultString);
            return Convert.ToInt32(stringValue);
        }

        public decimal readSettingDecimal(string key, decimal defaultValue)
        {
            string defaultString = Convert.ToString(defaultValue);
            string stringValue = readSetting(key, defaultString);
            return Convert.ToDecimal(stringValue);
        }


        public Color readSettingColor(string key, Color defaultValue)
        {
            Color returnColor = defaultValue;
            string colorSetting = readSetting(key, null);

            // ensure the color code is properly formatted.
            if (Regex.IsMatch(colorSetting, "^0[xX][0123456789aAbBcCdDeEfF]{6}$"))
            {
                // strip off the "0x" and prepend with "FF" Alpha channel
                string colorCode = "FF" + colorSetting.Substring(2);
                // convert to an Int32 for creating a proper color object
                int argb = Int32.Parse(colorCode, NumberStyles.HexNumber);
                returnColor = Color.FromArgb(argb);
            }

            return returnColor;
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // HPSettingsPanel
            // 
            this.Name = "HPSettingsPanel";
            this.Size = new System.Drawing.Size(229, 175);
            this.ResumeLayout(false);

        }
    }
}
